package cloudspace.controlpanel.filemanager;

import java.util.Collections;
import java.util.List;

import org.zkoss.zul.AbstractTreeModel;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;

import cloudspace.vm.filesystem.CSPath;


public class CSPathDirectoryTreeModel extends AbstractTreeModel
{
    // ----------------------------------------------------------
    public CSPathDirectoryTreeModel( FileManagerModel model )
    {
        super( model );

        this.model = model;
    }


    // ----------------------------------------------------------
    public Object getChild( Object object, int index )
    {
        if ( object == getRoot() )
        {
            return model.getRoots().get( index );
        }

        CSPath file = (CSPath)object;
        List<CSPath> children = file.getChildDirectories();
        Collections.sort( children, new DirectoriesFirstCSPathComparator() );
        return children.get( index );
    }


    // ----------------------------------------------------------
    public int getChildCount( Object object )
    {
        if ( object == getRoot() )
        {
            return model.getRoots().size();
        }
        else
        {
            CSPath file = (CSPath)object;
            List<CSPath> children = file.getChildDirectories();
            return children != null ? children.size() : 0;
        }
    }


    // ----------------------------------------------------------
    public boolean isLeaf( Object object )
    {
        if ( object instanceof CSPath )
        {
            CSPath path = (CSPath)object;

            if ( path.isFile() )
            {
                return true;
            }
        }

        return false;
    }


    // ----------------------------------------------------------
    public Treeitem addFolderToTree(
        Treeitem parentTreeitem,
        CSPath folder,
        boolean open )
    {
        Treeitem ti = new Treeitem();
        Treecell tc = new Treecell( folder.getName() );

        Treerow tr = new Treerow();
        tr.setParent( ti );
        tc.setParent( tr );
        ti.setValue( folder );
        ti.setOpen( true );
        ti.setImage( FileTypes.getFileIcon( folder ) );

        Treechildren treeChildren = parentTreeitem.getTreechildren();
        if ( treeChildren == null )
        {
            treeChildren = new Treechildren();
            treeChildren.setParent( parentTreeitem );
        }

        ti.setParent( treeChildren );
        return ti;
    }


    // ----------------------------------------------------------
    public void deleteFolderFromTree( Treeitem currentTreeitem, CSPath folder )
    {
        Treechildren treechildren = currentTreeitem.getTreechildren();

        int index = 0;
        Treeitem childToRemove = null;
        for ( Object component : treechildren.getItems() )
        {
            Treeitem ti = (Treeitem)component;
            CSPath file = (CSPath)ti.getValue();

            if ( file.equals( folder ) )
            {
                childToRemove = ti;
                break;
            }

            index++;
        }

        if ( childToRemove != null )
        {
            childToRemove.detach();
        }
    }


    // ----------------------------------------------------------
    public void renameFolderInTree(
        Treeitem currentTreeitem,
        CSPath from,
        CSPath to )
    {
        Treechildren treechildren = currentTreeitem.getTreechildren();
        for ( Object component : treechildren.getItems() )
        {
            Treeitem treeItem = (Treeitem)component;
            CSPath file = (CSPath)treeItem.getValue();

            if ( file.equals( from ) )
            {
                treeItem.setValue( to );
                Treerow treerow = treeItem.getTreerow();
                Treecell treecell = (Treecell)treerow.getChildren().get( 0 );
                treecell.setLabel( to.getName() );
                break;
            }
        }
        return;
    }


    // ----------------------------------------------------------
    public void moveTreeitem( Treeitem item, Treeitem destItem )
    {
        item.detach();

        CSPath destFolder = (CSPath)destItem.getValue();
        CSPath newFile = new CSPath( destFolder,
            ( (CSPath)item.getValue() ).getName() );

        addFolderToTree( destItem, newFile, true );
        // destItem.setOpen(true);
        destItem.invalidate();
    }

    private FileManagerModel model;

    private static final long serialVersionUID = 1L;
}
